<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getUserDetailById } from "@/api/user";
import { assignRoles } from '@/api/employees'
export default {
    props:{
        roleId:{
            type:String,
            required:true
        }
    },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
      //分配权限
      async hSubmit(){
        try {
          const res = await assignRoles({id:this.roleId,roleIds:this.roleIds})
          if(res.code !==10000) return this.$message.error(res.message)
          this.$message.success(res.message)
          this.loadRoles()
          this.$emit('close')
          // 保存数据
        } catch (error) {
          console.log(error)
        }
      },
      //获取角色列表
    async loadRoles() {
      try {
        const res = await getRoles({pagesize:100})
        console.log('getRoles', res)
        this.list = res.data.rows
        //发送请求获取当前用户拥有的角色,选中复选框
        const info = await getUserDetailById(this.roleId)
        console.log(info,'info')
        this.roleIds = info.data.roleIds || []
        // 保存数据
      } catch (err) {
        console.log('getRoles', err)
      }
    }
  }
}
</script>
